package org.tuoren.system.controller.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.system.domain.bo.SysVisitLogBo;
import org.tuoren.system.domain.vo.SysVisitLogVo;
import org.tuoren.system.service.ISysVisitLogService;

import java.util.List;

/**
 * 浏览日志记录
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/monitor/visitlog")
public class SysVisitLogController extends BaseController {
    private final ISysVisitLogService logService;
    /**
     * 获取浏览日志记录列表
     */
    @SaCheckPermission("monitor:visitlog:list")
    @GetMapping("/list")
    public TableDataInfo<SysVisitLogVo> list(SysVisitLogBo log, PageQuery pageQuery) {
        return logService.selectPageVisitLogList(log, pageQuery);
    }

    /**
     * 导出浏览日志记录列表
     */
    @Log(title = "浏览日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:visitlog:export")
    @PostMapping("/export")
    public void export(SysVisitLogBo operLog, HttpServletResponse response) {
        List<SysVisitLogVo> list = logService.selectVisitLogList(operLog);
        ExcelUtil.exportExcel(list, "浏览日志", SysVisitLogVo.class, response);
    }

    /**
     * 批量删除浏览日志记录
     * @param operIds 日志ids
     */
    @Log(title = "浏览日志", businessType = BusinessType.DELETE)
    @SaCheckPermission("monitor:visitlog:remove")
    @PostMapping("/{operIds}")
    public R<Void> remove(@PathVariable Long[] operIds) {
        return toAjax(logService.deleteVisitLogByIds(operIds));
    }

    /**
     * 清理浏览日志记录
     */
    @Log(title = "浏览日志", businessType = BusinessType.CLEAN)
    @SaCheckPermission("monitor:visitlog:remove")
    @PostMapping("/clean")
    public R<Void> clean() {
        logService.cleanVisitLog();
        return R.ok();
    }
}
